package com.example.order.controller;


import com.example.common.domain.PageResult;
import com.example.common.domain.Result;
import com.example.order.domain.dto.CreateOrderDTO;
import com.example.order.domain.dto.OrderListDTO;
import com.example.order.domain.dto.OrderPayDTO;
import com.example.order.domain.po.Order;
import com.example.order.domain.vo.OrderListVO;
import com.example.order.domain.vo.OrderVO;
import com.example.order.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "订单接口相关")
@Slf4j
@Validated
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    /**
     * 创建订单
     *
     * @param createOrderDTO
     * @return
     */
    @PostMapping
    public Result<String> createOrder(@Validated @RequestBody CreateOrderDTO createOrderDTO) {
        log.info("创建订单:{}", createOrderDTO);
        Long orderId = orderService.createOrder(createOrderDTO);
        return Result.success(orderId.toString());
    }

    /**
     * 获取订单详情
     *
     * @param orderId
     * @return
     */
    @GetMapping("/{orderId}")
    public Result<OrderVO> getOrder(@PathVariable @NotNull Long orderId) {
        log.info("获取订单详情:{}", orderId);
        return Result.success(orderService.getOrderDetail(orderId));
    }



    // 确认收货
    @PutMapping("/{orderId}/receipt")
    public Result<OrderVO> receiveOrder(@PathVariable @NotNull Long orderId) {
        log.info("确认收货:{}", orderId);
        OrderVO oorderVO = orderService.receiveOrder(orderId);
        return Result.success(oorderVO);
    }

    /**
     * 删除订单
     *
     * @param ids
     * @return
     */
    //TODO 删除订单
    @DeleteMapping
    public Result<String> deleteOrder(String[] ids) {
        log.info("删除订单:{}", ids);
        Long l = Long.valueOf(ids[0]);
        Long[] id = {l};
        orderService.deleteOrder(id);
        return Result.success("删除成功");
    }

    // 订单列表
    @GetMapping
    @Operation(summary = "订单列表")
    public Result<PageResult<OrderListVO>> getOrderList(@RequestParam(defaultValue = "1") String page,
                                                        @RequestParam(defaultValue = "5") String pageSize,
                                                        String status) {
        Integer pageNum = Integer.valueOf(page);

        Integer size = Integer.valueOf(pageSize);
        Integer orderStatus = Integer.valueOf(status);

        log.info("订单列表参数:页码:{},条数:{},状态:{}", pageNum, size, status);
        PageResult<OrderListVO> pageResult = orderService.getOrderList(pageNum, size, orderStatus);
        return Result.success(pageResult);
    }

    @PostMapping("/pay")
    public Result<Object> payOrder(@RequestBody OrderPayDTO payDTO) {
        log.info("支付订单:{}", payDTO);
        orderService.payOrder(payDTO.getOrderId());
        return Result.success();
    }

}
